//
//  DeleteExportViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DeleteExportViewController.h"
#import "AssetManager.h"

#define CELL_IMAGEVIEW  9901
#define CELL_LABEL      9902

@implementation DeleteExportViewController
@synthesize items;
@synthesize forImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)dealloc {
    [selected release];
    self.items = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(self.items != nil && [self.items count] > 0){
        selected = [[NSMutableArray alloc] initWithCapacity:[self.items count]];
        for (int i=0; i<[self.items count]; i++) {
            [selected addObject:[NSNumber numberWithBool:NO]];
        }
    }
    
    if(forImage == NO){
        btnExport.hidden = YES;
    }
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(IBAction)btnDelete_Click:(id)sender{
    AssetManager *am = [[AssetManager alloc] init];
    for (int i=0; i<[selected count]; i++) {
        BOOL isSelected = [[selected objectAtIndex:i] boolValue];
        if(isSelected){
            if (forImage) {
                [am deleteImage:[[self.items objectAtIndex:i] lastPathComponent]];
            }
            else{
                [am deleteVideo:[[self.items objectAtIndex:i] lastPathComponent]];
            }
        }
    }
    [am release];
    
    NSString *message = nil;
    if(forImage){
        if([selected count] > 1){
            message = @"Photos successfully removed.";
        }
        else{
            message = @"Photo successfully removed";
        }
    }
    else{
        if([selected count] > 1){
            message = @"Videos successfully removed.";
        }
        else{
            message = @"Video successfully removed.";
        }
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(IBAction)btnExport_Click:(id)sender{
    AssetManager *am = [[AssetManager alloc] init];
    for (int i=0; i<[selected count]; i++) {
        BOOL isSelected = [[selected objectAtIndex:i] boolValue];
        if(isSelected){
            if (forImage) {
                [am exportImage:[[self.items objectAtIndex:i] lastPathComponent]];
            }
            else{
                [am exportVideo:[[self.items objectAtIndex:i] lastPathComponent]];
            }
        }
    }
    [am release];
    
    NSString *message = nil;
    NSString *title = nil;
    if(forImage){
        title = @"Photo Transfer Success!";
        if([selected count] > 1){
            message = @"Your Photos were successfully transferred to the public Photo Library.";
        }
        else{
            message = @"Your Photo was successfully transferred to the public Photo Library.";
        }
    }
    else{
        title = @"Video Transfer Success!";
        if([selected count] > 1){
            message = @"Your Videos were successfully transferred to the public Video Library.";
        }
        else{
            message = @"Your Video was successfully transferred to the public Video Library.";
        }
    }
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [alert show];
    [alert release];
    
    [self.navigationController popViewControllerAnimated:YES];
}




#pragma marke table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.items != nil){
        return [self.items count];
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *item_cell_id = @"item_cell_id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:item_cell_id];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:item_cell_id] autorelease];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 2, 70, 40)];
        imageView.tag = CELL_IMAGEVIEW;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.backgroundColor = [UIColor clearColor];
        [cell addSubview:imageView];
        [imageView release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(76, 2, cell.frame.size.width-70-6-30, 40)];
        label.tag = CELL_LABEL;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor                    ];
        [cell addSubview:label];
        [label release];
        
    }
    
    BOOL isSelected = [[selected objectAtIndex:indexPath.row] boolValue];
    if(isSelected){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:CELL_IMAGEVIEW];
    UILabel *label = (UILabel *)[cell viewWithTag:CELL_LABEL];
    
    label.text = [[[self.items objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];
    
    if(forImage){
        imageView.image = [UIImage imageWithContentsOfFile:[self.items objectAtIndex:indexPath.row]];
    }
    else{
        imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@.jpg", [self.items objectAtIndex:indexPath.row]]];
    }
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BOOL isSelected = [[selected objectAtIndex:indexPath.row] boolValue];
    if(isSelected){
        isSelected = NO;
        [selected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isSelected]];
    }
    else{
        isSelected = YES;
        [selected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isSelected]];
    }
    [tvItems reloadData];
}

@end
